package com.group56.model.dao.Impl;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.group56.model.dao.CustomerDAO;
import com.group56.model.entity.Customer;
import com.group56.model.tools.jsonFileProcessor;
import com.group56.utils.DBSrcMenu;

import java.util.ArrayList;

public class CustomerDAOImpl implements CustomerDAO {

    public static void main(String[] args) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.CUSTOMER_JSON.getSrc());
        new CustomerDAOImpl().update(new Customer("","2","2","2","2","2","2","2","2","2"),"2");
        ArrayList<Customer> records = new CustomerDAOImpl().getAll(file);
        System.out.println(records);
    }

    public ArrayList<Customer> getAll(jsonFileProcessor file){
        StringBuffer buffer = file.read();
        if (!buffer.toString().equals(""))
            return new Gson().fromJson(buffer.toString(), new TypeToken<ArrayList<Customer>>() {}.getType());
        return null;
    }

    public boolean insert(Customer customer) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.CUSTOMER_JSON.getSrc());
        ArrayList<Customer> records = getAll(file);
        if (records!=null && selectByID(customer.getCustomerID())==null) {
            if (records.size()!=0)
                customer.setCustomerID(String.valueOf(Integer.parseInt(records.get(records.size() - 1).getCustomerID())+1));
            else
                customer.setCustomerID("1");
            records.add(customer);
            return file.rewrite(records);
        }
        return false;
    }

    public boolean delete(String customerID) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.CUSTOMER_JSON.getSrc());
        ArrayList<Customer> records = getAll(file);
        if (records!=null) {
            for (Customer c : records)
                if (c.getCustomerID().equals(customerID)){
                    records.remove(c);
                    return file.rewrite(records);
                }
        }
        return false;
    }

    public boolean update(Customer customer, String customerID) {
        jsonFileProcessor file = new jsonFileProcessor(DBSrcMenu.CUSTOMER_JSON.getSrc());
        ArrayList<Customer> records = getAll(file);
        if (records!=null) {
            for (int i=0;i<records.size();i++){
                if (records.get(i).getCustomerID().equals(customerID)) {
                    customer.setCustomerID(customerID);
                    records.set(i, customer);
                    return file.rewrite(records);
                }
            }
        }
        return false;
    }

    public Customer selectByID(String customerID) {
        ArrayList<Customer> records = getAll(new jsonFileProcessor(DBSrcMenu.CUSTOMER_JSON.getSrc()));
        if (records!=null) {
             for (Customer c : records)
                if (c.getCustomerID().equals(customerID))
                    return c;
        }
        return null;
    }

    public Customer selectByName(String name){
        ArrayList<Customer> records = getAll(new jsonFileProcessor(DBSrcMenu.CUSTOMER_JSON.getSrc()));
        if (records!=null) {
            for (Customer c : records)
                if (c.getName().equals(name))
                    return c;
        }
        return null;
    }
}
